home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 114 / macaddict114.cdr / Software / Utilities / macam.0.8.4.dmg / macam sources / utilities / yuv2rgb.h < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-25  |  3.8 KB  |  81 lines

  1. /*
  2.     macam - webcam app and QuickTime driver component
  3.     Copyright (C) 2002 Matthias Krauss (macam@matthias-krauss.de)
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  $Id: yuv2rgb.h,v 1.2 2002/10/25 10:00:36 mattik Exp $
  19. */
  20.  
  21. #ifndef _YUV2RGB_
  22. #define _YUV2RGB_
  23.  
  24. #include "GlobalDefs.h"
  25. #include <stdbool.h>
  26.  
  27. /*
  28.  
  29. Here is a conversion function for yuv -> rgb. YUV is a chorominance-subsampling bitmap format commonly used in video applications. The idea is that chominance is not as important as luminance since human eyes cannot see colors that sharp (please forgive me this simplification!). 4:2:0 means that chominance is subsampled by a factor of two both horizontally and vertically. There is a variety of opinions how YUV420 should be stored.
  30.  
  31.  
  32.  Opinion 1 (Philips)
  33.  
  34.  Line 1: YYYYUU
  35.  Line 2: YYYYVV (where each letter represents a byte so we have 8 pixels stored in 12 bytes here)
  36.  
  37.  
  38.  Opinion 2 (CPIA - YUYV variant)
  39.  
  40.  Line 1: YUYV
  41.  Line 2: YY     (where each letter represents a byte so we have 4 pixels stored in 6 bytes here)
  42.  
  43.  Opinion 3 (not supported): Planar.
  44.  
  45.  Opinion 4-n (not supported): still to come with the next manufacturers
  46.  
  47. There is a hefty dicussion out there if the preferred format for video within the machine should be yuv or rgb. These diffeences are a good reason to find my personal decision: rgb. It's more data, but definitely not subject to these differences (I know, RGB, RGBA, ARGB, AGBR... I will ignore this. We're on a Mac).
  48.  
  49. We have the problem that this function should be fast. Of course, it could all be done with one big function that ifs and switches for the different types, but this would be slow. So I took another approach: For each conversion combination, there will be a blitting function. The main function just dispatches between them. This way, we have to do the format decision only one and not for every single pixel - I don't know much about performance tuning, but the rules I had in mind were:
  50.  
  51. - many memory access commands are bad (because they are slow)
  52. - non-linear memory access is bad (because it causes memory cache page misses)
  53. - branches in inner loops are bad (because they might jam the processor pipelines)
  54. - commands that need the results of the previous command are bad (because it degrades processor out of order scheduling)
  55. - pointer casts are ok (because they are handled in the compiler)
  56.  
  57. Because it would be obviously too bad style to write the function for every single combination, so there are files containing the function bodies (for each source format, there's one file). They can be configured using #defines. They are included several times with different define settings to unfold to the functions. This is perhaps not the best style possible, but a quite good compromise.
  58.  
  59. Someone interested in AltiVec optimization?
  60.  
  61. */
  62.  
  63. typedef enum YUVStyle {
  64.     YUVPhilipsStyle    =0,
  65.     YUVCPIA420Style    =1,
  66.     YUVCPIA422Style    =2,
  67.     YUVOV420Style    =3
  68. } YUVStyle;
  69.  
  70.  void yuv2rgb(int width,
  71.               int height,
  72.               YUVStyle style,
  73.               unsigned char *src,
  74.               unsigned char *dst,
  75.               short bpp,
  76.               long srcRowExtra,
  77.               long dstRowExtra,
  78.               bool flip);
  79.  
  80. #endif
  81.